package com.szu.subject.domains;

import java.util.List;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class Student extends User {

	private List<Course> courses;// 课程
	private List<ClassTime> classTimes;// 上课时间
	private List<String> timesString;// 上课时间
	private Long classId;
	private Double stuCredit;// 学分
	// private StudentClass studentClass;
	private Long minorCollege;// 辅修
	private Double hasCredit;
	public Student() {
		setRole("Student");
	}

	public List<Course> getCourses() {
		return courses;
	}

	public void setCourses(List<Course> courses) {
		this.courses = courses;
	}

	public List<ClassTime> getClassTimes() {
		return classTimes;
	}

	public void setClassTimes(List<ClassTime> classTimes) {
		this.classTimes = classTimes;
	}

	public long getClassId() {
		return classId;
	}

	public void setClassId(long classId) {
		this.classId = classId;
	}

	public Double getStuCredit() {
		return stuCredit;
	}

	public void setStuCredit(Double stuCredit) {
		this.stuCredit = stuCredit;
	}

	public Long getMinorCollege() {
		return minorCollege;
	}

	public List<String> getTimesString() {
		return timesString;
	}

	public void setTimesString(List<String> timesString) {
		this.timesString = timesString;
	}

	public void setMinorCollege(Long minorCollege) {
		this.minorCollege = minorCollege;
	}

	public void setClassId(Long classId) {
		this.classId = classId;
	}
	
	
	@JsonIgnore
	public Double getHasCredit() {
		if(courses==null||courses.size()==0)
			return 0d;
//		if(hasCredit==0)
		setHasCredit();
		return hasCredit;
	}

	public void setHasCredit(Double hasCredit) {
		this.hasCredit += hasCredit;
	}
	
	public void setHasCredit() {
		for(Course course:courses){
			hasCredit+=course.getCredit();
		}
	}

	/**
	 * 是否能选这门课。 只判断时间和已有课程 是否达到上限不能选由数据库判断
	 * 
	 * @param course
	 * @return
	 */
	public boolean isAllow(Course course) {
		if (this.courses.contains(course))// 不能选同一门课程
			return false;

		String[] courseTime = course.getTimes().split(",");
		for (String string : courseTime) {
			if (this.timesString.contains(string))// 时间不重叠
				return false;
		}
		// 辅修
		if (minorCollege != null && course.getCollegeId().equals(minorCollege)) {
			if (course.isPrimaryAvailable()) {
				return true;
			} else if (course.isSecondaryAvailable()) {
				return true;
			} else {
				return false;
			}
		}
		if (isPrimary(course)) {
			if (course.isPrimaryAvailable()) {
				return true;
			} else {
				return false;
			}
		} else {
			if (course.isSecondaryAvailable()) {
				return true;
			} else {
				return false;
			}
		}
	}

	// 是否主选
	public boolean isPrimary(Course course) {
		String classIds = course.getClassIds();
		if (classIds == null) {
			return true;
		} else {
			String[] classIdString = classIds.split(",");
			if (classIdString.length == 0){
				return true;
			}
			else {
				for (String s : classIdString) {
					if (classId.equals(s)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	// 辅修是否主选
	public boolean isMinorPrimary(Course course) {
		if (minorCollege != null && course.getCollegeId().equals(minorCollege)) {
			return true;
		} else
			return false;
	}
}
